<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="关联订单id" prop="relatedData">
        <el-input
          v-model="queryParams.relatedData"
          placeholder="请输入关联订单id或序列号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-download" size="mini" @click="exportData"
          >导出</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="tableList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="id" prop="id" width="200" />
      <el-table-column label="反馈内容" prop="reasonDesc" />
      <el-table-column label="反馈类型" prop="feedbackType">
        <template slot-scope="scope">
          {{ feedbackTypeMap[scope.row.feedbackType] }}
        </template>
      </el-table-column>
      <el-table-column label="反馈状态" prop="feedbackStatus">
        <template slot-scope="scope">
          {{ feedbackStatusMap[scope.row.feedbackStatus] }}
        </template>
      </el-table-column>
      <el-table-column label="关联数据" prop="relatedData" />
      <el-table-column label="处理内容" prop="solvedDesc" />
      <el-table-column label="处理人员" prop="solvedUser" />
      <el-table-column label="处理时间" prop="solvedTime" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="200"
      >
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSolve(scope.row)"
            v-hasPermi="['charge:site:edit']"
            >处理</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[10, 20, 30, 50, 500, 1000]"
    />

    <!-- 处理反馈 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- 处理内容 -->
        <el-form-item label="处理内容" prop="solvedDesc">
          <el-input
            v-model="form.solvedDesc"
            type="textarea"
            placeholder="请输入处理内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getListAPI, solveAPI } from "@/api/charge/feedback";
import { exportExcel, exportExcelFromAPI } from "@/utils/utils";

export default {
  name: "Site",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      tableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],

      feedbackTypeMap: {
        SITE_STATION: "设备",
        CHARGE_RECORD: "充电记录",
        ORDERS: "订单",
      },
      feedbackStatusMap: {
        DOING: "处理中",
        DONE: "已完成",
      },

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        relatedData: undefined,
      },
      // 表单参数
      form: {},

      // 表单校验
      rules: {
        solvedDesc: [
          { required: true, message: "处理内容不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    exportData() {
      exportExcelFromAPI("/feedback/list/export", {
        ...this.queryParams,
        pageNum: 1,
        pageSize: 10000,
      });
      exportExcel({
        data: this.formatTableList(this.tableList),
        headers: [
          { label: "id", key: "id" },
          { label: "反馈内容", key: "reasonDesc" },
          { label: "反馈类型", key: "feedbackType" },
          { label: "反馈状态", key: "feedbackStatus" },
          { label: "关联数据", key: "relatedData" },
          { label: "处理内容", key: "solvedDesc" },
          { label: "处理人员", key: "solvedUser" },
          { label: "处理时间", key: "solvedTime" },
          { label: "创建时间", key: "createTime" },
        ],
        filename: "投诉反馈数据.xlsx",
      });
    },
    // 格式化表格数据
    formatTableList(data) {
      return data.map((item) => {
        return {
          ...item,
          feedbackType: this.feedbackTypeMap[item.feedbackType],
          feedbackStatus: this.feedbackStatusMap[item.feedbackStatus],
          createTime: this.parseTime(item.createTime),
        };
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      getListAPI(this.queryParams).then((res) => {
        this.tableList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 处理按钮操作 */
    handleSolve(row) {
      this.reset();
      this.form = {
        id: row.id,
        solvedDesc: row.solvedDesc,
      };
      this.open = true;
      this.title = "处理反馈";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          solveAPI(this.form).then((res) => {
            this.$modal.msgSuccess("处理成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
